package com.qf.mybatis;

import com.qf.mybatis.mapper.UserMapper;
import com.qf.mybatis.pojo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class HelloMybatis {
    SqlSession sqlSession = null;
    @Test
    public void demo1() throws IOException {
//        //1.获得读取MyBatis配置文件的流对象
//        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//
//        //2.构建SqlSession连接对象的工厂
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //3.通过工厂获得连接对象
//        SqlSession sqlSession = factory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        Student student = mapper.selectUserById(1);

        System.out.println(student);
    }
    @Before
    public void before() throws IOException {
        //1.获得读取MyBatis配置文件的流对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

        //2.构建SqlSession连接对象的工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

        //3.通过工厂获得连接对象
        sqlSession = factory.openSession();
    }
    public void demo2(){

    }
}
